<?php

class GoogleMapService{
/**
 * Demo of getting Lat and Lon from Google Maps.
 * @author Abdullah Rubiyath <http://itsalif.info>
 * 
 * Use & distribute it Freely
 */

  private $key = 'ABQIAAAAitcYEaGEy3dFqkIFOe8oMBTh2bIvPlfRdQrtomDQ5y1LqYBXrxRecjUzPvcEmI2NI6Wp2piRp4_40w';
  

	public function getLatitudeLongitudeByLocationName($locationName){
		$opt = array (
			'address'	=> urlencode($locationName) ,
			'output'	=> 'xml' 
		);
		
		$url = 'http://maps.google.com/maps/geo?q='.$opt['address'].'&output='.$opt['output'].'&oe=utf8&key='.$this->key;
		
		$dom = new DOMDocument();
		$dom->load($url);
			
		$xpath = new DomXPath($dom);
		$xpath->registerNamespace('ge', 'http://earth.google.com/kml/2.0');
			
		$statusCode = $xpath->query('//ge:Status/ge:code');
			
		if ($statusCode->item(0)->nodeValue == '200') {
			
			$pointStr = $xpath->query('//ge:coordinates');
			$point = explode(",", $pointStr->item(0)->nodeValue);
			
			$coordinate['latitude'] = $point[1];
			$coordinate['longitude'] = $point[0];
		
			return $coordinate;
		}	
	}


}

?>